/**
  author: kevin
 */
package controller

import (
	"net/http"
	"log"
	"dooms/supplychain/service"
	"io/ioutil"
	"encoding/base64"
)

type Application struct {
	Fabric *service.ServiceSetup
}


func (app *Application) QueryOrderView(w http.ResponseWriter, r *http.Request)  {
	showView(w, r, "queryOrder.html", nil)
}

// QueryOrder 查询订单状态
func (app *Application) QueryOrder(w http.ResponseWriter, r *http.Request) {
	order := r.FormValue("order")
	
	status, err := app.Fabric.GetOrder(order)
	data := &struct {
		Status string
		Msg		string
	}{
		Status: "",
		Msg: "",
	}

	if err != nil {
		data.Status = ""
		data.Msg = "对不起，服务器出现错误:" + err.Error()
	}else{
		if status == "1"{
			data.Status = "已发货"
		}else if status == "2"{
			data.Status = "已确认"
		}else if status == "3"{
			data.Status = "已收货"
		}else if status == "4"{
			data.Status = "已融资"
		}
	}
	showView(w, r, "queryOrder.html", data)
}

// ChangeOrder 更改订单状态
func(app *Application) ChangeOrder(w http.ResponseWriter, r *http.Request) {
	order := r.FormValue("order")
	status := r.FormValue("status")

	app.Fabric.SetOrder(order, status)

	app.QueryOrder(w, r)
}

// ChangeOrder 发货
func(app *Application) ChangeOrderSend(w http.ResponseWriter, r *http.Request) {
	order := r.FormValue("order")
	status := r.FormValue("status")

	app.Fabric.SetOrderSend(order, status)

	app.QueryOrder(w, r)
}

// ChangeOrder 确认
func(app *Application) ChangeOrderRecv(w http.ResponseWriter, r *http.Request) {
	order := r.FormValue("order")
	status := r.FormValue("status")

	app.Fabric.SetOrderRecv(order, status)

	app.QueryOrder(w, r)
}

// ChangeOrder 收货
func(app *Application) ChangeOrderRecv1(w http.ResponseWriter, r *http.Request) {
	order := r.FormValue("order")
	status := r.FormValue("status")

	app.Fabric.SetOrderRecv1(order, status)

	app.QueryOrder(w, r)
}

func (app *Application) RequestLoanView(w http.ResponseWriter, r *http.Request)  {
	showView(w, r, "requestloan.html", nil)
}

// RequestLoan 申请贷款
func (app *Application) RequestLoan(w http.ResponseWriter, r *http.Request) {
	project := r.FormValue("project")
	time := r.FormValue("time")
	num := r.FormValue("num")

	txid, err := app.Fabric.RequestLoan(project, time, num)

	// 封装响应数据
	data := &struct {
		Flag bool
		Msg string
	}{
		Flag:true,
		Msg:"",
	}
	if err != nil {
		data.Msg = err.Error()
	}else{
		data.Msg = "操作成功，交易ID: " + txid
	}

	// 响应客户端
	showView(w, r, "requestloan.html", data)

}


func (app *Application) LoanView(w http.ResponseWriter, r *http.Request)  {
	showView(w, r, "loan.html", nil)
}

// Loan 发放贷款
func (app *Application) Loan(w http.ResponseWriter, r *http.Request) {
	project := r.FormValue("project")
	loanid := r.FormValue("loanid")
	loantime := r.FormValue("loantime")
	loannum := r.FormValue("loannum")

	txid, err := app.Fabric.Loan(project, loanid, loantime, loannum)

	// 封装响应数据
	data := &struct {
		Flag bool
		Msg string
	}{
		Flag:true,
		Msg:"",
	}
	if err != nil {
		data.Msg = err.Error()
	}else{
		data.Msg = "操作成功，交易ID: " + txid
	}

	// 响应客户端
	showView(w, r, "loan.html", data)

}


func (app *Application) SetQuotaView(w http.ResponseWriter, r *http.Request)  {
	showView(w, r, "setquota.html", nil)
}

// SetQuota 设置贷款
func (app *Application) SetQuota(w http.ResponseWriter, r *http.Request) {
	project := r.FormValue("project")
	quota := r.FormValue("quota")

	txid, err := app.Fabric.SetQuota(project, quota)

	// 封装响应数据
	data := &struct {
		Flag bool
		Msg string
	}{
		Flag:true,
		Msg:"",
	}
	if err != nil {
		data.Msg = err.Error()
	}else{
		data.Msg = "操作成功，交易ID: " + txid
	}

	// 响应客户端
	showView(w, r, "setquota.html", data)

}


func (app *Application) QueryHistoryView(w http.ResponseWriter, r *http.Request)  {
	showView(w, r, "queryhistory.html", nil)
}

// QueryHistory 查询交易历史
func (app *Application) QueryHistory(w http.ResponseWriter, r *http.Request) {
	project := r.FormValue("project")
	key := r.FormValue("type")

	if key == "loan"{
		project = "loan_" + project
	}else if key == "requestloan" {
		project = "query_loan_" + project
	}

	info, err := app.Fabric.QueryHistory(project)

	log.Println("info:", info)

	// 封装响应数据
	data := &struct {
		Flag bool
		Msg string
	}{
		Flag:true,
		Msg:"",
	}
	if err != nil {
		data.Msg = err.Error()
	}else{
		data.Msg = info
	}

	// 响应客户端
	showView(w, r, "queryhistory.html", data)

}

// UpLoadView 上传文件
func (app *Application) UpLoadView(w http.ResponseWriter, r *http.Request)  {
	if r.Method == "GET" {
		showView(w, r, "upload.html", nil)
	}else{
		r.ParseMultipartForm(32 << 20)
		file, _, _ := r.FormFile("file_head")
		buf,_:= ioutil.ReadAll(file)
		defer file.Close()
		encode := base64.StdEncoding.EncodeToString(buf)
		//decode, _ := base64.StdEncoding.DecodeString(encode)
		_, err := app.Fabric.Upload(encode)
		if err != nil{
			w.Write([]byte("Failed" + err.Error()))
		}else{
			w.Write([]byte("OK"))
		}
	}
}

// DownloadPicture 查看图片
func (app *Application) DownloadPicture(w http.ResponseWriter, r *http.Request)  {

	pic, err := app.Fabric.Download("")

	decode, _ := base64.StdEncoding.DecodeString(pic)
	if err != nil{
		w.Write([]byte("Failed"))
	}else{
		w.Write(decode)
	}

}